﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Text;
using Texfina.Core.Common;
using Texfina.Common.Mg;
using Texfina.Common.Rh;
using Texfina.Core.Web;
using Texfina.Core.Data;
using Texfina.Entity.Rh;
using Texfina.Entity.Mg;
using Texfina.Entity.Sy;
using RHBOQry = Texfina.BOQry.Rh;
using MGBOQry = Texfina.BOQry.Mg;
using RHBOMnt = Texfina.BOMnt.Rh;
using SYBOQry = Texfina.BOQry.Sy;

namespace RH
{
   public partial class wfConcepRemuMnt : EditPage
   {

      public const int _GRDLIST_IDCONREMU = 2;
      public const int _GRDLIST_STANULADO = 5;
      public const string _STEDITFORMCAB = "0";
      public const string _STEDITFORMDET = "1";

      /// <summary>
      /// Guarda el estado de un string, se usa para saber que boton llama al editor de formularios
      /// </summary>
      /// <value></value>
      /// <returns></returns>
      public string StEditForm
      {
         get { return (string)this.ViewState["_dsStEF"]; }
         set { this.ViewState["_dsStEF"] = value; }
      }

      public string DsFormHra
      {
         get { return (string)this.ViewState["_dsFormHra"]; }
         set { this.ViewState["_dsFormHra"] = value; }
      }

      public string DsFormula
      {
         get { return (string)this.ViewState["_dsFormula"]; }
         set { this.ViewState["_dsFormula"] = value; }
      }

      public string DsCondition
      {
         get { return (string)this.ViewState["_dsCondition"]; }
         set { this.ViewState["_dsCondition"] = value; }
      }

      private string IdEditor
      {
         get { return this.ViewState["_idEditor"].ToString(); }
         set { this.ViewState["_idEditor"] = value; }
      }

      private string NuSeccion
      {
         get { return this.ViewState["_nuSec"].ToString(); }
         set { this.ViewState["_nuSec"] = value; }
      }

      public DataTable DSCampos
      {
         get { return (DataTable)this.ViewState["_dsCampos"]; }
         set { this.ViewState["_dsCampos"] = value; }
      }

      protected void Page_Load(object sender, EventArgs e)
      {
         if (this.IsPostBack)
         {
            this.LoadInPostBack();
            return;
         }
         this.LoadData();
      }

      public override void LoadInPostBack()
      {
         this.RegisterGeneralScripts();
      }

      public override void LoadInitialSettings()
      {
         this.FormEditAction = (FormEditAction)Enum.Parse(typeof(FormEditAction), Request.QueryString["formEA"]);
         Label lbl = (Label)this.Master.FindControl("lblTitle");
         lbl.Text = "Conceptos Remunerativos";
      }

      public override void LoadListControls()
      {
         ETablaGenDet objEDet = new ETablaGenDet();
         MGBOQry.ITablaGen objBOI = (MGBOQry.ITablaGen)WCFHelper.GetObject<MGBOQry.ITablaGen>(typeof(MGBOQry.TablaGen));

         objEDet.IdTabla = ConstMultitabla.TIPO_CONCEPTO_PLANILLA;
         DataTable dt = objBOI.GetDetailByList(objEDet);
         WebHelper.LoadListControl(ddlidTipConcep, dt, "dsDescrip", "idCodigo", true, true);

         objEDet.IdTabla = ConstMultitabla.TIPO_REMUN_FIJA_VARIABLE;
         DataTable dta = objBOI.GetDetailByList(objEDet);
         WebHelper.LoadListControl(ddlidTipRemu, dta, "dsDescrip", "idCodigo", true, true);

      }

      public override void DataToControls()
      {
         EConcepRemu objE = new EConcepRemu();
         IBOQuery objBO = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(RHBOQry.ConcepRemu));
         objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
         objE.IdConRemu = Request.QueryString["IdConRemu"];
         objE = (EConcepRemu)objBO.GetByKey(objE);

         if (objE != null)
         {
            WebHelper.EntityToControl(objE, "IdConRemu", txtidConRemu);
            WebHelper.EntityToControl(objE, "DsConRemu", txtdsConRemu);
            WebHelper.EntityToControl(objE, "IdTipConcep", ddlidTipConcep);
            this.DsFormHra = objE.DsFormHra;
            this.DsFormula = objE.DsFormula;
            this.DsCondition = objE.DsCondition;
            WebHelper.EntityToControl(objE, "StUsaFormu", chkStUsaFormu);
            WebHelper.EntityToControl(objE, "StCtaCte", chkstCtaCte);
            WebHelper.EntityToControl(objE, "StProcAdic", chkStProcAdic);
            WebHelper.EntityToControl(objE, "StAfectaGrati", chkstAfecGrati);
            WebHelper.EntityToControl(objE, "IdTipoRemu", ddlidTipRemu);
         }

         EConRemuDet objEDet = new EConRemuDet();
         RHBOQry.IConcepRemu objBODet = (RHBOQry.IConcepRemu)WCFHelper.GetObject<RHBOQry.IConcepRemu>(typeof(RHBOQry.ConcepRemu));
         objEDet.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
         objEDet.IdConRemu = this.txtidConRemu.Text;
         DataTable dt = objBODet.GetDetailByParentKey(objEDet);
         this.GridSetDataBinding(grdList, dt);

         EConRemuCampo objECam = new EConRemuCampo();
         objECam.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
         objECam.IdConremu = this.txtidConRemu.Text;
         dt = objBODet.GetCamposParentKey(objECam);

         this.DSCampos = dt;
      }

      public override void LockControls()
      {
         txtidConRemu.Attributes.Add("readonly", "true");
      }

      public override void RegisterGeneralScripts()
      {

         StringBuilder sb = new StringBuilder();

         sb.AppendLine("function validateEditFormPopup(key){");
         sb.AppendLine("switch(key)");
         sb.AppendLine("{");
         sb.AppendLine("case '" + ConstMenuToolbarValue.BUTTON_SAVE + "':");
         sb.AppendLine("case '_BTTEVAL':");
         sb.AppendLine("var varRpta = isEmptyControl('" + this.txtdsFormula.ClientID + "','La Fórmula no puede estar vacia');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("menu_doPostBack('" + mnuPopup.ClientID + "','" + mnuPopup.UniqueID + "',key);");
         sb.AppendLine("break;");
         sb.AppendLine("case '" + ConstMenuToolbarValue.BUTTON_EXIT + "': ");
         sb.AppendLine("$find('" + this.mdlPopup.ClientID + "').hide();");
         sb.AppendLine("return false;");
         sb.AppendLine("break;");
         sb.AppendLine("}");
         sb.AppendLine("return false;");
         sb.AppendLine("}");

         sb.AppendLine("function validateEditForm(key){");
         sb.AppendLine("      var varRpta = isEmptyControl('" + this.txtdsConRemu.ClientID + "','Ingrese la Descripción');");
         sb.AppendLine("      if(varRpta==true)");
         sb.AppendLine("      return !varRpta;");
         sb.AppendLine("menu_doPostBack('" + mnuToolbar.ClientID + "','" + mnuToolbar.UniqueID + "',key);");
         sb.AppendLine("}");

         sb.AppendLine("function OnChange" + this.chkStUsaFormu.ID + "(){");
         sb.AppendLine("   if(document.getElementById('" + chkStUsaFormu.ClientID + "').checked){ ");
         sb.AppendLine("      document.getElementById('" + this.bttFormula.ClientID + "').disabled = false;");
         sb.AppendLine("   } else {");
         sb.AppendLine("      document.getElementById('" + this.bttFormula.ClientID + "').disabled = true;");
         sb.AppendLine("   } return false;");
         sb.AppendLine("}");

         ScriptManager.RegisterStartupScript(this, this.GetType(), "ScriptRegister", sb.ToString(), true);

         this.chkStUsaFormu.Attributes.Add("onClick", "OnChange" + chkStUsaFormu.ID + "();");
      }

      public void RegisterEditPopupScripts()
      {

      }

      protected void mnuToolbar_MenuItemClick(Object sender, MenuEventArgs e)
      {
         if (e.Item == null)
            return;

         switch (e.Item.Value)
         {
            case ConstMenuToolbarValue.BUTTON_SAVE:
            case ConstMenuToolbarValue.BUTTON_SAVENEW:
               if (this.FormEditAction != FormEditAction.Delete)
                  this.SaveData(e.Item.Value);
               else
                  this.DeleteData();
               break;

            case ConstMenuToolbarValue.BUTTON_EXIT:
               //FILTER
               this.Response.Redirect("wfConcepRemuBnd.aspx?Filter=1");
               //FILTER END
               break;
         }

      }

      public override void SaveData(String key)
      {
         if (this.ValidateData() == false)
            return;

         IEntityBase objE = (IEntityBase)this.CreateEntitySet();

         IBOUpdate objPro = (IBOUpdate)WCFHelper.GetObject<IBOUpdate>(typeof(RHBOMnt.ConcepRemu));

         String[] strRet = objPro.UpdateData(objE);
         String strMsg = "";

         switch (this.FormEditAction)
         {

            case FormEditAction.AddNew:
            case FormEditAction.Copy:
               strMsg = "Se ha registrado el Concepto Remunerativo : " + strRet[0].ToString();
               break;
            default:
               strMsg = "Se ha modificado el Concepto Remunerativo : " + this.txtdsConRemu.Text;
               break;
         }
         this.hdnMsgOPB.Value = this.CreatePostbackMessage("CORRECTO", strMsg, TypeMessageAlert.correct, false, "wfConcepRemuBnd.aspx?Filter=1");

      }

      protected void mnuGrid_MenuItemClick(Object sender, MenuEventArgs e)
      {
         if (e.Item == null)
            return;

         switch (e.Item.Value)
         {
            case ConstMenuToolbarValue.BUTTON_NEW:
               this.GridEditAction = GridEditAction.AddNew;
               this.StEditForm = _STEDITFORMDET;
               this.lblTitlePopup.Text = "Editor de Formulas";
               this.mdlPopup.Show();
               this.LoadDataEditPopup(this.grdList, -1);
               break;
         }

      }

      protected void mnuPopup_MenuItemClick(Object sender, MenuEventArgs e)
      {
         if (e.Item == null)
            return;

         switch (e.Item.Value)
         {
            case ConstMenuToolbarValue.BUTTON_SAVE:

               switch (this.StEditForm)
               {
                  case _STEDITFORMCAB:

                     if (!this.FormulaEvalSec1(false))
                     {
                        mdlPopup.Show();
                        return;
                     }

                     this.DsFormHra = this.txtdsFormHra.Text;
                     this.DsFormula = this.txtdsFormula.Text;
                     this.DsCondition = this.txtdsCondition.Text;
                     break;

                  case _STEDITFORMDET:

                     Object objResNuSec = null;
                     int intResNusec = 0;
                     DataTable dt = (DataTable)this.DataSource;
                     DataRow dr = null;

                     if (this.GridEditAction == GridEditAction.AddNew || this.GridEditAction == GridEditAction.Copy)
                     {
                        objResNuSec = dt.Compute("MAX(nuSecuen)", "");
                        if (objResNuSec == null || objResNuSec.ToString() == "")
                           intResNusec = 0;
                        else
                           intResNusec = Convert.ToInt32(objResNuSec);
                        intResNusec += 1;
                        dr = dt.NewRow();
                        dr["nuSecuen"] = intResNusec;
                     }
                     else
                     {
                        dr = this.GridGetRowFromDataKey(grdList, this.EditRowIndex);
                     }

                     if (this.GridEditAction != GridEditAction.Delete)
                     {
                        dr["idEmpresa"] = Session[ConstSessionVar.EMPRESA].ToString();
                        dr["idConRemu"] = this.txtidConRemu.Text;
                        dr["dsFormula"] = this.txtdsFormula.Text;
                        dr["dsCondition"] = this.txtdsCondition.Text;
                        dr["stAnulado"] = ConstFlagEstado.DESACTIVADO;

                     }
                     else
                     {
                        dr["stAnulado"] = ConstFlagEstado.ACTIVADO;
                     }

                     if (this.GridEditAction == GridEditAction.AddNew || this.GridEditAction == GridEditAction.Copy)
                     {
                        dt.Rows.Add(dr);
                     }


                     this.GridSetDataBinding(grdList, dt);

                     break;
               }
               break;

            case "_BTTEVAL":
               if (!this.FormulaEvalSec1(true))
               {
                  mdlPopup.Show();
                  return;
               }
               mdlPopup.Show();
               hdnMsgOPB.Value = this.CreatePostbackMessage("CORRECTO", "La Formula es correcta", TypeMessageAlert.correct, false, "");
               ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "msgPB", "MessageOnPostback();", true);
               break;

            case ConstMenuToolbarValue.BUTTON_EXIT:
               break;
         }

      }

      protected void bttFormula_Click(object sender, EventArgs e)
      {
         this.GridEditAction = GridEditAction.AddNew;
         this.StEditForm = _STEDITFORMCAB;
         this.lblTitlePopup.Text = "Editor de Formulas";
         this.mdlPopup.Show();
         this.IdEditor = ConstFormularioEditor.FORM2;
         this.NuSeccion = ConstFormularioEditorSeccion.SEC1;
         this.LoadDataEditPopup(this.grdList, -1);
      }

      public override void LoadDataEditPopup(GridView grid, int rowIndex)
      {
         this.ClearEditPopup();
         this.LockControlsPopup();

         ETablaCF objE = new ETablaCF();
         objE.IdEditor = this.IdEditor;
         objE.NuSeccion = this.NuSeccion;

         RHBOQry.ITablaCF objBO = WCFHelper.GetObject<RHBOQry.ITablaCF>(typeof(RHBOQry.TablaCF));

         DataTable dt = objBO.GetCamposXSeccion(objE);
         DataView dv = new DataView(dt);

         TreeNode nodP = new TreeNode("Campos", ConstTipoVariable.CAMPO);
         nodP.NavigateUrl = "javascript:void(0)";
         dv.RowFilter = "idType='" + ConstTipoVariable.CAMPO + "'";
         foreach (DataRowView drv in dv)
         {
            TreeNode nod = new TreeNode(drv["dsField"].ToString());
            nod.NavigateUrl = "javascript:void(0);";
            nod.ToolTip = drv["dsToolTip"].ToString();
            nodP.ChildNodes.Add(nod);
         }
         tvwCampoFor.Nodes.Add(nodP);

         nodP = new TreeNode("Variables", ConstTipoVariable.VARIABLE);
         nodP.NavigateUrl = "javascript:void(0)";
         dv.RowFilter = "idType='" + ConstTipoVariable.VARIABLE + "'";
         foreach (DataRowView drv in dv)
         {
            TreeNode nod = new TreeNode(drv["dsField"].ToString());
            nod.NavigateUrl = "javascript:void(0);";
            nod.ToolTip = drv["dsToolTip"].ToString();
            nodP.ChildNodes.Add(nod);
         }
         tvwCampoFor.Nodes.Add(nodP);

         nodP = new TreeNode("Acumuladores", ConstTipoVariable.ACUMULADOR);
         nodP.NavigateUrl = "javascript:void(0)";
         dv.RowFilter = "idType='" + ConstTipoVariable.ACUMULADOR + "'";
         foreach (DataRowView drv in dv)
         {
            TreeNode nod = new TreeNode(drv["dsField"].ToString());
            nod.NavigateUrl = "javascript:void(0);";
            nod.ToolTip = drv["dsToolTip"].ToString();
            nodP.ChildNodes.Add(nod);
         }
         tvwCampoFor.Nodes.Add(nodP);

         nodP = new TreeNode("Condicionales", ConstTipoVariable.CONDICION);
         nodP.NavigateUrl = "javascript:void(0)";
         dv.RowFilter = "idType='" + ConstTipoVariable.CONDICION + "'";
         foreach (DataRowView drv in dv)
         {
            TreeNode nod = new TreeNode(drv["dsField"].ToString());
            nod.NavigateUrl = "javascript:void(0);";
            nod.ToolTip = drv["dsToolTip"].ToString();
            nodP.ChildNodes.Add(nod);
         }
         tvwCampoFor.Nodes.Add(nodP);

         switch (this.StEditForm)
         {
            case _STEDITFORMCAB:

               this.txtdsFormHra.Text = this.DsFormHra;
               this.txtdsFormula.Text = this.DsFormula;
               this.txtdsCondition.Text = this.DsCondition;
               break;

            case _STEDITFORMDET:

               DataRow dr = this.GridGetRowFromDataKey(this.DataSource, grdList, rowIndex);
               if (dr == null)
                  return;

               this.txtdsFormula.Text = dr["dsFormula"].ToString();
               this.txtdsCondition.Text = dr["dsCondition"].ToString();

               break;
         }
      }

      protected void grdList_RowCommand(Object sender, GridViewCommandEventArgs e)
      {
         this.EditRowIndex = Convert.ToInt32(e.CommandArgument);
         this.StEditForm = _STEDITFORMDET;
         this.lblTitlePopup.Text = "Editor de Formulas";
         switch (e.CommandName)
         {

            case ConstGridRowCommand.GRIDEDIT:


               this.GridEditAction = GridEditAction.Edit;
               this.mdlPopup.Show();
               this.LoadDataEditPopup(grdList, this.EditRowIndex);
               this.RegisterEditPopupScripts();
               break;
            case ConstGridRowCommand.GRIDDELETE:
               this.GridEditAction = GridEditAction.Delete;
               this.mdlPopup.Show();
               this.LoadDataEditPopup(grdList, this.EditRowIndex);
               this.RegisterEditPopupScripts();
               break;
         }
      }

      protected void grdList_RowDataBound(Object sender, GridViewRowEventArgs e)
      {
         if (e.Row.RowType == DataControlRowType.DataRow)
         {
            String strRI = e.Row.RowIndex.ToString();

            ImageButton bttedit = (ImageButton)e.Row.FindControl("grdBtt" + ConstGridRowCommand.GRIDEDIT);
            ImageButton bttdelete = (ImageButton)e.Row.FindControl("grdBtt" + ConstGridRowCommand.GRIDDELETE);

            bttedit.OnClientClick = "javascript:return validateGridCommands('" + ConstGridRowCommand.GRIDEDIT + "','" + strRI + "');";
            bttdelete.OnClientClick = "javascript:return validateGridCommands('" + ConstGridRowCommand.GRIDDELETE + "','" + strRI + "');";


            if (e.Row.Cells[_GRDLIST_STANULADO].Text == ConstFlagEstado.ACTIVADO)
            {
               e.Row.ForeColor = System.Drawing.Color.Red;
               bttdelete.ImageUrl = "../Images/Icons/Disabled/Delete.png";
               bttedit.ImageUrl = "../Images/Icons/Disabled/Edit.png";
            }
         }

      }

      public new void ClearEditPopup()
      {
         tvwCampoFor.Nodes.Clear();
         this.txtdsFormHra.Text = "";
         this.txtdsFormula.Text = "";
         this.txtdsCondition.Text = "";
      }

      public void LockControlsPopup()
      {

         if (this.GridEditAction == GridEditAction.Delete)
         {
            this.txtdsFormHra.Enabled = false;
            this.txtdsFormula.Enabled = false;
            this.txtdsCondition.Enabled = false;
         }
         else
         {
            if (this.StEditForm == _STEDITFORMDET)
            {
               this.txtdsFormHra.Enabled = false;
               this.txtdsFormula.Enabled = true;
               this.txtdsCondition.Enabled = true;
            }
            else
            {
               this.txtdsFormHra.Enabled = true;
               this.txtdsFormula.Enabled = true;
               this.txtdsCondition.Enabled = true;
            }
         }
      }

      public override void DeleteData()
      {
         IEntityBase objE = (IEntityBase)this.CreateEntitySet();
         IBOUpdate objPro = (IBOUpdate)WCFHelper.GetObject<IBOUpdate>(typeof(RHBOMnt.Personal));
         objPro.DeleteData(objE);
         this.hdnMsgOPB.Value = this.CreatePostbackMessage("CORRECTO", "Se ha eliminado el concepto " + this.txtdsConRemu.Text, TypeMessageAlert.correct, false, "wfConcepRemuBnd.aspx?Filter=1");
      }

      public override Object CreateEntitySet()
      {
         EConcepRemu objE = new EConcepRemu();

         if (this.FormEditAction == FormEditAction.AddNew || this.FormEditAction == FormEditAction.Copy)
            objE.EntityState = EntityState.Added;
         else
         {
            IBOQuery objBO = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(RHBOQry.ConcepRemu));
            objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
            objE.IdConRemu = this.txtidConRemu.Text;
            objE = (EConcepRemu)objBO.GetByKey(objE);

            if (this.FormEditAction == FormEditAction.Edit)
               objE.EntityState = EntityState.Modified;
            if (this.FormEditAction == FormEditAction.Delete)
               objE.EntityState = EntityState.Deleted;

         }

         objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
         objE.IdConRemu = this.txtidConRemu.Text;
         WebHelper.ControlToEntity(txtidConRemu, objE, "IdConRemu");
         WebHelper.ControlToEntity(txtdsConRemu, objE, "DsConRemu");
         WebHelper.ControlToEntity(ddlidTipConcep, objE, "IdTipConcep");
         WebHelper.ControlToEntity(chkStUsaFormu, objE, "StUsaFormu");
         if (chkStUsaFormu.Checked)
         {
            objE.DsFormHra = this.DsFormHra;
            objE.DsFormula = this.DsFormula;
            objE.DsCondition = this.DsCondition;
         }
         else
         {
            objE.DsFormHra = "";
            objE.DsFormula = "";
            objE.DsCondition = "";
         }
         WebHelper.ControlToEntity(chkstCtaCte, objE, "StCtaCte");
         WebHelper.ControlToEntity(chkStProcAdic, objE, "StProcAdic");
         WebHelper.ControlToEntity(chkstAfecGrati, objE, "StAfectaGrati");
         WebHelper.ControlToEntity(ddlidTipRemu, objE, "IdTipoRemu");

         if (this.FormEditAction == FormEditAction.Delete)
            objE.StAnulado = ConstFlagEstado.ACTIVADO;
         else
            objE.StAnulado = ConstFlagEstado.DESACTIVADO;

         DataTable dt = (DataTable)this.DataSource;

         this.ClearDeletedRow(this.DataSource, "stAnulado");
         IList<EConRemuDet> colE = new List<EConRemuDet>();

         if (this.FormEditAction == FormEditAction.Delete)
         {
            foreach (DataRow dr in dt.Rows)
               this.MarkDeletedRow(dr, "stAnulado");
         }

         for (int intTypeUpd = 0; intTypeUpd <= 1; intTypeUpd++)
         {
            DataTable dtChange = null;
            if (intTypeUpd == 0)
               dtChange = dt.GetChanges(DataRowState.Added);
            else
               dtChange = dt.GetChanges(DataRowState.Modified);

            if (dtChange != null)
            {
               foreach (DataRow drDet in dtChange.Rows)
               {
                  EConRemuDet objEDet = DataHelper.CopyDataRowToEntity<EConRemuDet>(drDet, typeof(EConRemuDet));

                  if (drDet.RowState == DataRowState.Added)
                     objEDet.EntityState = EntityState.Added;

                  if (drDet.RowState == DataRowState.Modified)
                  {
                     if (objEDet.StAnulado == ConstFlagEstado.DESACTIVADO)
                        objEDet.EntityState = EntityState.Modified;
                     else
                        objEDet.EntityState = EntityState.Deleted;
                  }

                  colE.Add(objEDet);
               }
            }
         }

         IList<EConRemuCampo> colCampo = new List<EConRemuCampo>();
         foreach (DataRow dr in this.DSCampos.Rows)
         {
            EConRemuCampo objEDet = DataHelper.CopyDataRowToEntity<EConRemuCampo>(dr, typeof(EConRemuCampo));
            objEDet.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
            objEDet.IdConremu = txtidConRemu.Text;
            objEDet.EntityState = EntityState.Added;

            colCampo.Add(objEDet);
         }

         EConRemuSet objESet = new EConRemuSet();
         objESet.ConcepRemu = objE;
         objESet.ConRemuDet = colE;
         objESet.ConRemuCampo = colCampo;
         return objESet;
      }

      public bool FormulaEvalSec1(bool onlyEval)
      {

         FormulaParser objParser = new FormulaParser(this.Page);

         try
         {
            if (txtdsFormHra.Text != "")
            {
               foreach (TreeNode nod in tvwCampoFor.Nodes[0].ChildNodes)
                  objParser.Fields.Add(new FormulaField(nod.Text, nod.Parent.Value));
               foreach (TreeNode nod in tvwCampoFor.Nodes[1].ChildNodes)
                  objParser.Fields.Add(new FormulaField(nod.Text, nod.Parent.Value));
               foreach (TreeNode nod in tvwCampoFor.Nodes[2].ChildNodes)
                  objParser.Fields.Add(new FormulaField(nod.Text, nod.Parent.Value));

               objParser.ParseFormula(txtdsFormHra.Text);
               objParser.Fields.Clear();
            }

            foreach (TreeNode nod in tvwCampoFor.Nodes[0].ChildNodes)
               objParser.Fields.Add(new FormulaField(nod.Text, nod.Parent.Value));
            foreach (TreeNode nod in tvwCampoFor.Nodes[1].ChildNodes)
               objParser.Fields.Add(new FormulaField(nod.Text, nod.Parent.Value));
            foreach (TreeNode nod in tvwCampoFor.Nodes[2].ChildNodes)
               objParser.Fields.Add(new FormulaField(nod.Text, nod.Parent.Value));

            objParser.ParseFormula(txtdsFormula.Text);

            objParser.Fields.Clear();
            foreach (TreeNode nod in tvwCampoFor.Nodes[3].ChildNodes)
               objParser.Fields.Add(new FormulaField(nod.Text, nod.Parent.Value));

            if (txtdsCondition.Text != "")
               objParser.ParseCondition(txtdsCondition.Text);

            if (!onlyEval)
            {
               this.DSCampos.Rows.Clear();
               this.DSCampos.AcceptChanges();

               foreach (FormulaField objF in objParser.FieldsUsed)
               {
                  if (objF.Type == ConstTipoVariable.VARIABLE || objF.Type == ConstTipoVariable.ACUMULADOR)
                  {
                     StringBuilder sb = new StringBuilder(objF.Key);

                     sb.Replace("{", "");
                     sb.Replace("}", "");

                     DataRow dr = this.DSCampos.NewRow();
                     dr.BeginEdit();
                     dr["idEmpresa"] = Session[ConstSessionVar.EMPRESA].ToString();
                     dr["idConremu"] = txtidConRemu.Text;
                     dr["idCampo"] = sb.ToString();
                     dr["nuNivel"] = 0;
                     dr["idTipo"] = objF.Type;
                     dr.EndEdit();
                     this.DSCampos.Rows.Add(dr);
                  }
               }
            }

         }
         catch (Exception ex)
         {
            hdnMsgOPB.Value = this.CreatePostbackMessage("ERROR", ex.Message, TypeMessageAlert.error, false, "");
            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "msgPB", "MessageOnPostback();", true);
            return false;
         }

         return true;
      }
   }
}